package com.yao.sdk.glide;

import android.util.Log;

import com.yao.sdk.encrypt.MD5;

/**
 * Created by Administrator on 2016/9/21.
 */
public class QiNiuHandle {
    /*
        七牛签名key
     */
    public static final String IMG_KEY = "3caa16b91aaba18a789b3da82daa6e8b96a9e1a9";
    public static final String VIDEO_KEY = "758a11889f8a92a8f4bbe698728acb9879894d88";
    public static final String GOODS_KEY = "189b6e99199abc8b61980299da89ed88429903a8";
    /*
        七牛域名
     */
    public static final String IMG_IP = "http://img.goonpa.com";
    public static final String VIDEO_IP = "http://video.goonpa.com";
    public static final String GOODS_IP = "http://goods.goonpa.com";
    public static final int OVER_DATE = 5;

    public static String sign(String url, int width, int height) {
        String domain = getDomain(url);
        if (domain == null) {
            return "";
        }
        String key = getKey(domain);

        int overTime = (int) (System.currentTimeMillis() / 1000 + 3600 * 24 * OVER_DATE);
        String timeTag = Integer.toHexString(overTime).toLowerCase();
        String sign = MD5.getMD5Str(key+url.replaceFirst(domain,"")+timeTag).toLowerCase();
        String size="imageView2/1/w/"+width+"/h/"+height;
        return url+"?"+size+"/sign="+sign+"&t="+timeTag;
    }
    public static String sign(String url) {
        String domain = getDomain(url);
        if (domain == null) {
            return "";
        }
        String key = getKey(domain);

        int overTime = (int) (System.currentTimeMillis() / 1000 + 3600 * 24 * OVER_DATE);
        String timeTag = Integer.toHexString(overTime).toLowerCase();
        String newUrl=url.replaceFirst(domain,"");
        String sign = MD5.getMD5Str(key+newUrl+timeTag).toLowerCase();

        return url+"?sign="+sign+"&t="+timeTag;
    }
    public static String getDomain(String url) {
        if (url.startsWith(IMG_IP)) {
            return IMG_IP;
        } else if (url.startsWith(VIDEO_IP)) {
            return VIDEO_IP;
        } else if (url.startsWith(GOODS_IP)) {
            return GOODS_IP;
        } else {
            return null;
        }
    }

    public static String getKey(String domain) {
        String key;
        switch (domain) {
            case IMG_IP:
                key = IMG_KEY;
                break;
            case VIDEO_IP:
                key = VIDEO_KEY;
                break;
            case GOODS_IP:
                key = GOODS_KEY;
                break;
            default:
                key = null;
                break;
        }
        return key;
    }
}
